package com.wkhyapp.lm.fragment;

import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.shizhefei.view.indicator.IndicatorViewPager;
import com.shizhefei.view.indicator.ScrollIndicatorView;
import com.shizhefei.view.indicator.slidebar.ColorBar;
import com.shizhefei.view.indicator.transition.OnTransitionTextListener;
import com.wkhyapp.lm.R;
import com.wkhyapp.lm.base.BaseFragment;
import com.wkhyapp.lm.base.SuperFragment;
import com.wkhyapp.lm.contract.DynamicPresenter;
import com.wkhyapp.lm.contract.DynamicView;
import com.wkhyapp.lm.http.vo.Goods;
import com.wkhyapp.lm.utils.DensityUtil;
import com.wkhyapp.lm.view.SearchActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class DynamicFragment extends SuperFragment<DynamicPresenter> implements DynamicView {


    @BindView(R.id.tabs)
    ScrollIndicatorView scrollIndicatorView;
    @BindView(R.id.viewpager_main)
    ViewPager viewPager;
    private IndicatorViewPager indicatorViewPager;
    private List<BaseFragment> fragments = new ArrayList<>();
    private List<String> titles = new ArrayList<>();
    DynamicFragment.MyAdapter adapter;
    @BindView(R.id.sousuo_rl)
    RelativeLayout sousuo_rl;
    @Override
    protected DynamicPresenter createPresenter() {
        return mPresenter = new DynamicPresenter(this);
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_dynamic;
    }

    @Override
    public void initUI() {
        fragments.add(new DTFragment());
        fragments.add(new JXFragment());
        fragments.add(new GZFragment());
        fragments.add(new FSFragment());
        titles.add("动态");
        titles.add("精选商家");
        titles.add("关注");
        titles.add("粉丝");
        float unSelectSize = 14;
        float selectSize = 15 * 1.2f;
        scrollIndicatorView.setOnTransitionListener(new OnTransitionTextListener().setColor(0xFF000000, 0xFF999999).setSize(selectSize, unSelectSize));
        scrollIndicatorView.setScrollBar(new ColorBar(getContext(), 0xFFFFEC48, 6));
        viewPager.setOffscreenPageLimit(4);
        indicatorViewPager = new IndicatorViewPager(scrollIndicatorView, viewPager);
        adapter = new DynamicFragment.MyAdapter(getChildFragmentManager());
        indicatorViewPager.setAdapter(adapter);
    }



    @Override
    protected void initData() {
    }

    @Override
    protected void initListener() {
        sousuo_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goTo(SearchActivity.class);
            }
        });
    }


    @Override
    public void setGoodsMore(List<Goods> goodsMore) {

    }

    private class MyAdapter extends IndicatorViewPager.IndicatorFragmentPagerAdapter {

        public MyAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public View getViewForTab(int position, View convertView, ViewGroup container) {
            if (convertView == null) {
                convertView =  getActivity().getLayoutInflater().inflate(R.layout.tab_top, container, false);
            }
            TextView textView = (TextView) convertView;
            textView.setText(titles.get(position));

            int witdh = getTextWidth(textView);
            int padding = DensityUtil.dipToPix(getContext(), 5);
            //因为wrap的布局 字体大小变化会导致textView大小变化产生抖动，这里通过设置textView宽度就避免抖动现象
            //1.3f是根据上面字体大小变化的倍数1.3f设置
            textView.setWidth((int) (witdh * 1.3f) + padding);

            return convertView;
        }

        @Override
        public Fragment getFragmentForPage(int position) {
            return fragments.get(position);
        }


        @Override
        public int getItemPosition(Object object) {
            //这是ViewPager适配器的特点,有两个值 POSITION_NONE，POSITION_UNCHANGED，默认就是POSITION_UNCHANGED,
            // 表示数据没变化不用更新.notifyDataChange的时候重新调用getViewForPage
            return PagerAdapter.POSITION_NONE;
        }

        private int getTextWidth(TextView textView) {
            if (textView == null) {
                return 0;
            }
            Rect bounds = new Rect();
            String text = textView.getText().toString();
            Paint paint = textView.getPaint();
            paint.getTextBounds(text, 0, text.length(), bounds);
            int width = bounds.left + bounds.width();
            return width;
        }

    }

    @Override
    public void setGoods(List<Goods> goodsList) {

    }


}
